var gulp = require('gulp')
var sass = require('gulp-sass')
var sourcemaps = require('gulp-sourcemaps')
var autoprefixer = require('gulp-autoprefixer')
var plumber = require('gulp-plumber')
var concat = require('gulp-concat')
var uglify = require('gulp-uglify')
var browserSync = require('browser-sync').create()
var reload = browserSync.reload

gulp.task('vendor-js', function() {
    return gulp.src([
        './node_modules/jquery/dist/jquery.min.js',
        // './node_modules/bootstrap/dist/js/bootstrap.min.js',
        // './node_modules/formBuilder/dist/form-builder.min.js',
        // './node_modules/formBuilder/dist/form-render.min.js'
    ])
    .pipe(gulp.dest('./assets/js'))
})

gulp.task('vendor-font', function () {
  return gulp.src([
    './node_modules/bootstrap/dist/fonts/*'
  ])
  .pipe(gulp.dest('./assets/fonts'))
})

gulp.task('vendor-css', function () {
  return gulp.src([
    // './node_modules/bootstrap/dist/css/bootstrap.min.css',
    './node_modules/formBuilder/dist/form-builder.min.css',
    './node_modules/formBuilder/dist/form-render.min.css'
  ])
  .pipe(gulp.dest('./assets/css'))
})

gulp.task('sass', function () {
  return gulp.src('./src/sass/**/*.scss')
    .pipe(plumber())
    .pipe(sourcemaps.init())
    .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
    .pipe(sourcemaps.write())
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(autoprefixer({browsers: ['last 2 versions']}))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('./assets/css'))
    .pipe(reload({stream: true}))
})

gulp.task('js', function () {
  return gulp.src([
    // './src/js/config/language.js',
    // './src/js/config/*.js',
    './src/js/app.js'
  ])
  .pipe(plumber())
  .pipe(sourcemaps.init())
  .pipe(concat('app.js'))
  .pipe(uglify())
  .pipe(sourcemaps.write('.'))
  .pipe(gulp.dest('./assets/js'))
  .pipe(reload({stream: true}))
})

gulp.task('server', ['sass', 'js'], function () {
    browserSync.init({
        server: '.'
    })
    gulp.watch('./src/sass/**/*.scss', ['sass'])
    gulp.watch('./src/js/**/*.js', ['js'])

    gulp.watch("*.html").on('change', reload)
})

gulp.task('build', ['sass', 'js'])

gulp.task('default', ['server'])
